package com.atguigu.controller;


import com.atguigu.entity.AlbumAttributeValue;
import com.atguigu.entity.AlbumInfo;
import com.atguigu.entity.BaseAttribute;
import com.atguigu.login.TingShuLogin;
import com.atguigu.mapper.AlbumInfoMapper;
import com.atguigu.mapper.AlbumStatMapper;
import com.atguigu.mapper.BaseAttributeMapper;
import com.atguigu.query.AlbumInfoQuery;
import com.atguigu.result.RetVal;
import com.atguigu.service.AlbumAttributeValueService;
import com.atguigu.service.AlbumInfoService;
import com.atguigu.service.BaseCategoryViewService;
import com.atguigu.util.AuthContextHolder;
import com.atguigu.vo.AlbumListVo;
import com.atguigu.vo.AlbumStatVo;
import com.atguigu.vo.CategoryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 一级分类表 前端控制器
 * </p>
 *
 * @author Joe
 * @since 2024-11-15
 */
@Tag(name = "专辑管理")
@RestController
@RequestMapping("/api/album/albumInfo")
public class AlbumController {
    //category视图
    @Autowired
    private AlbumInfoService albumInfoService;
    @Autowired
    private AlbumInfoMapper albumInfoMapper;



    @TingShuLogin
    @Operation(summary = "新增专辑")
    @PostMapping("saveAlbumInfo")
    public RetVal saveAlbumInfo(@RequestBody AlbumInfo albumInfo){
        albumInfoService.saveAlbumInfo(albumInfo);
        return RetVal.ok();
    }


    //TODO id问题
    //@TingShuLogin
    @Operation(summary = "分页查询专辑")
    @PostMapping("getUserAlbumByPage/{page}/{limit}")
    public RetVal getUserAlbumByPage(
            @Parameter(name="page",description = "当前页码",required = true)
            @PathVariable Long page,
            @Parameter(name="limit",description = "每页记录数",required = true)
            @PathVariable Long limit,
            @Parameter(name="albumInfoQuery",description = "查询对象",required = false)
            AlbumInfoQuery albumInfoQuery){
        //Long userId = AuthContextHolder.getUserId();
        Long userId = 15l;
        System.out.println(userId);
        albumInfoQuery.setUserId(userId);
        IPage<AlbumListVo> pageParam = new Page<>(page, limit);
        pageParam = albumInfoMapper.getUserAlbumByPage(pageParam,albumInfoQuery);
        return RetVal.ok(pageParam);
    }


    @Operation(summary = "根据id查询专辑")
    @GetMapping("getAlbumInfoById/{albumId}")
    public RetVal<AlbumInfo> getAlbumInfoById(@PathVariable Long albumId){
        AlbumInfo albumInfo = albumInfoService.getAlbumInfoById(albumId);
        return RetVal.ok(albumInfo);
    }

    @Operation(summary = "修改专辑")
    @PutMapping("updateAlbumInfo")
    public RetVal updateAlbumInfo(@RequestBody AlbumInfo albumInfo){
        albumInfoService.updateAlbumInfo(albumInfo);
        return RetVal.ok(albumInfo);
    }

    @Operation(summary = "删除专辑")
    @DeleteMapping("deleteAlbumInfo/{albumId}")
    public RetVal deleteAlbumInfo(@PathVariable Long albumId){
        albumInfoService.deleteAlbumInfo(albumId);
        return RetVal.ok();
    }

    //以下内容属于搜索模块
    @Autowired
    private AlbumAttributeValueService albumAttributeValueService;
    @Operation(summary = "根据albumId查询专辑属性")
    @GetMapping("getAlbumPropertyValue/{albumId}")
    public List<AlbumAttributeValue> getAlbumPropertyValue(@PathVariable Long albumId){
        LambdaQueryWrapper<AlbumAttributeValue> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AlbumAttributeValue::getAlbumId,albumId);

        List<AlbumAttributeValue> albumAttributeValueList = albumAttributeValueService.list(wrapper);

        return albumAttributeValueList;
    }
    @Autowired
    private AlbumStatMapper albumStatMapper;
    @Operation(summary = "获取专辑统计信息")
    @GetMapping("getAlbumStatInfo/{albumId}")
    public AlbumStatVo getAlbumStatInfo(@PathVariable Long albumId){
        AlbumStatVo albumStatVo = albumStatMapper.getAlbumStatInfo(albumId);
        return albumStatVo;
    }

    @Operation(summary = "是否订阅")
    @GetMapping("isSubscribe/{albumId}")
    public RetVal isSubscribe(@PathVariable Long albumId){
        boolean flag = albumInfoService.isSubscribe(albumId);
        return RetVal.ok(flag);
    }


}
